#ifndef DOT_UTIL__H
#define DOT_UTIL__H
	#include <stdio.h>
	#include "structures/node.h"

 #define SIZE_MAX 255
	void open(FILE *f)
	{
		fprintf(f, "digraph G {\n");
	}
	void createnode(FILE* f,node* id,char *text)
    {
        char *s=text;
        int a_free=0;
        // printf("dot_create in \n");
        switch(id->type)
        {
            case TPROGRAM:
            case TMAIN :
            case TOPBINAIRE:
            case TMOINSUNAIRE:
            case TNON:
            case TIF:
            case TWHILE :
            case TARRAY:
            case TRETURN:
            case TCST:
            case TVAR:
            {
                s=text;
                break;
            }
            case TCALLFUN :
            {
                asprintf(&s,"call fun %s",text);
                a_free =1;
                break;
            }
            case TPROCEDURE:
            {
                asprintf(&s,"procedure %s",text);
                a_free =1;
                break;
            }
            case TFUNCTION :
            {
                asprintf(&s,"fonction %s",text);
                a_free =1;
                break;
            }
        }
       // printf("chaine creer : %p -> %s \n",s,s);
        fprintf(f, "\"%p\" [label=\"%s\"];\n",id,s);
        if(a_free == 1)
        {
            free(s);
        }
        //printf("dot_create out \n");
	}
	void linknode(FILE* f,node* from,node * to)
	{
        fprintf(f,"\"%p\" -> \"%p\";\n",from,to);
	}
	void close(FILE *f)
	{
		fprintf(f," \n};\n");
		fclose(f);
	}

    void dot_DFS(node *a,FILE* f)
    {
        if(a!=NULL)
        {
            createnode(f,a,a->lexeme);
            if(a->enfants != NULL)
            {
                node *child = a->enfants;
                while(child)
                {
                    linknode(f,a,child);
                    child = child -> frere;
                }
            }
            dot_DFS(a->enfants,f);

            dot_DFS(a->frere,f);
        }

    }

#endif
